<template>
  <el-card class="box-card">
    <div>
      <!-- 搜索框 -->
      <el-form
        ref="searchData"
        :inline="true"
        :model="searchData"
        class="demo-form-inline"
      >
        <el-form-item label="名称" style="width: 20%">
          <el-input
            v-model="searchData.name"
            clearable
            placeholder="菜单名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="URL" style="width: 20%">
          <el-input
            v-model="searchData.url"
            clearable
            placeholder="菜单URL"
          ></el-input>
        </el-form-item>
        <el-form-item label="父菜单" style="width: 22%">
          <el-select v-model="searchData.parentId" clearable placeholder="请选择父菜单">
            <el-option
              v-for="{menuId,name} in parentMenus"
              :key="menuId"
              :label="name"
              :value="menuId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类型" style="width: 20%">
          <el-select v-model="searchData.type" clearable placeholder="请选择菜单类型">
            <el-option
              v-for="{type,name} in types"
              :key="type"
              :label="name"
              :value="type"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <br>
        <el-form-item>
          <el-button
            icon="el-icon-search"
            type="primary"
            @click="getDataListBySearch"
          >查询
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" @click="openLookWindow">新增</el-button>
        </el-form-item>
      </el-form>
      <!-- 角色数据表单 -->
      <el-table
        :data="dataList"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        border
        row-key="menuId"
        style="width: 100%"
      >
        <el-table-column label="菜单编号" prop="menuId" width="110">
        </el-table-column>
        <el-table-column label="父菜单ID" prop="parentId" width="120">
        </el-table-column>
        <el-table-column label="菜单名称" prop="name" width="130">
        </el-table-column>
        <el-table-column label="菜单URL" prop="url" width="230">
        </el-table-column>
        <el-table-column label="类型" width="80">
          <template v-slot="scope">
            <div v-for="{type,name} in types" :key="type">
              <span v-if="type === scope.row.type ">{{name}}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="图标" prop="icon" width="50">
          <template v-slot="scope">
            <i :class="`el-icon-${scope.row.icon}`"></i>
          </template>
        </el-table-column>
        <el-table-column label="排序" prop="orderNum" width="130">
        </el-table-column>
        <el-table-column label="操作">
          <template v-slot="scope">
            <el-button
              size="mini"
              type="primary"
              @click="openEditWindow(scope.row)"
            >编辑
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="deleteMenu(scope.row)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 新增按钮打开的页面 -->
      <el-dialog :visible.sync="lookWindow" title="新增菜单">
        <el-form :model="insertData">
          <el-form-item label="菜单名称" label-width="20%">
            <el-input
              v-model="insertData.name"
              autocomplete="off"
              placeholder="菜单名称"
              style="width: 80%"
            ></el-input>
          </el-form-item>
          <el-form-item label="菜单URL" label-width="20%">
            <el-input
              v-model="insertData.url"
              autocomplete="off"
              placeholder="菜单URL"
              style="width: 80%"
            ></el-input>
          </el-form-item>
           <el-form-item label="菜单类型" label-width="20%">
            <el-select v-model="insertData.type" placeholder="请选择">
              <el-option
                v-for="{type,name} in types"
                :key="type"
                :label="name"
                :value="type"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="菜单图标" label-width="20%">
            <el-select v-model="insertData.icon" clearable filterable placeholder="请选择">
              <el-row style="width: 100%;">
                <el-col
                  v-for="item in icons"
                  :key="item.icon"
                  :span="6"
                >
                  <el-option
                    v-for="{icon,name} in item"
                    :key="icon"
                    :label="name"
                    :value="icon"
                    class="el-option"
                  >
                    <i :class="`el-icon-${icon}`"/>
                  </el-option>
                </el-col>
              </el-row>
            </el-select>
          </el-form-item>
          <el-form-item label="排序" label-width="20%">
            <el-input
              v-model="insertData.orderNum"
              placeholder="默认为 1 "
              style="width: 80%"
              type="number"
            ></el-input>
          </el-form-item>
          <el-form-item label="父菜单" label-width="20%">
            <el-select v-model="insertData.parentId" placeholder="默认无父菜单">
              <el-option
                v-for="{menuId,name} in parentMenus"
                :key="menuId"
                :label="name"
                :value="menuId"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="访问权限" label-width="20%">
            <el-select v-model="insertData.access" placeholder="访问菜单需要的权限">
              <el-option
                v-for="{roleId,roleName,access} in roles"
                :key="roleId"
                :label="roleName"
                :value="access"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="lookWindow = false;">取 消</el-button>
          <el-button type="primary" @click="insertMenu()">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 编辑按钮打开的页面 -->
      <el-dialog :visible.sync="editWindow" title="编辑菜单">
        <el-form :model="editData">
          <el-form-item label="菜单名称" label-width="20%">
            <el-input
              v-model="editData.name"
              autocomplete="off"
              placeholder="菜单名称"
              style="width: 80%"
            ></el-input>
          </el-form-item>
          <el-form-item label="菜单URL" label-width="20%">
            <el-input
              v-model="editData.url"
              autocomplete="off"
              placeholder="菜单URL"
              style="width: 80%"
            ></el-input>
          </el-form-item>
           <el-form-item label="类型" label-width="20%">
            <el-select v-model="editData.type" placeholder="">
              <el-option
                v-for="{type,name} in types"
                :key="type"
                :label="name"
                :value="type"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="菜单图标" label-width="20%">
            <el-select v-model="editData.icon" clearable filterable placeholder="请选择">
              <el-row style="width: 100%">
                <el-col
                  v-for="item in icons"
                  :key="item.icon"
                  :span="6"
                >
                  <el-option
                    v-for="{icon,name} in item"
                    :key="icon"
                    :label="name"
                    :value="icon"
                    class="el-option"
                  >
                    <i :class="`el-icon-${icon}`"/>
                  </el-option>
                </el-col>
              </el-row>
            </el-select>
          </el-form-item>
          <el-form-item label="排序" label-width="20%">
            <el-input
              v-model="editData.orderNum"
              autocomplete="off"
              placeholder="排序"
              style="width: 80%"
              type="number"
            ></el-input>
          </el-form-item>
          <el-form-item v-if="editData.type !== 0" label="父菜单" label-width="20%">
            <el-select
              v-model="editData.parentId"
              placeholder="请选择父菜单"
            >
              <el-option
                v-for="{menuId,name} in parentMenus"
                :key="menuId"
                :label="name"
                :value="menuId"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="访问权限" label-width="20%">
            <el-select v-model="editData.access">
              <el-option
                v-for="{roleId,roleName,access} in roles"
                :key="roleId"
                :label="roleName"
                :value="access"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="editWindow = false">取 消</el-button>
          <el-button type="primary" @click="updateMenu()">更 新</el-button>
        </div>
      </el-dialog>
      <!-- 页号 -->
      <el-pagination
        :current-page="pageIndex"
        :page-size="pageSize"
        :page-sizes="pageSizes"
        :total="totalPage"
        layout="total ,sizes,prev,pager,next,jumper"
        style="margin-top: 30px"
        @size-change="sizeChangeHandle"
        @current-change="CurrentChangeHandle"
      >
      </el-pagination>
    </div>
  </el-card>
</template>
<script>

import {setRouter} from "@/router";

export default {
  name: "SysMenu",
  data() {
    return {
      //数据参数
      searchData: {},//搜索数据
      editData: {}, //编辑数据
      insertData: {},//新增数据

      //集合参数
      icons: [], //添加是可以选择的图标
      dataList: [], //页面展示的数据集合
      parentMenus: [], //父级菜单
      types: [], //菜单类型
      roles: [], //角色信息菜单

      //页号参数
      pageIndex: 1,
      pageSizes: [],
      pageSize: 5,
      totalPage: 0,
      begin: 0,
      end: this.pageSize - 1,

      //窗口显示参数
      lookWindow: false, //默认关闭新建菜单界面
      editWindow: false, //默认关闭编辑菜单界面

      //其他
      dataListFrom: "getDataList",//当前数据来源于搜索还是全局
    };
  },
  methods: {
    //改变数组大小
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.SizeOrCurrentChangeAfter();
    },
    //改变当前页号
    CurrentChangeHandle(val) {
      this.pageIndex = val;
      this.SizeOrCurrentChangeAfter();
    },
    //在更改数组大小或者页号前，判断数据来源
    SizeOrCurrentChangeAfter() {
      if (this.dataListFrom === "getDataList")
        this.getDataList();
      else this.getDataListBySearch();
    },

    //打开新增页面
    openLookWindow() {
      //刷新表格
      this.insertData = {};
      //清空父级菜单
      this.parentMenus = [];
      //获取父菜单
      this.getParentMenu();
      //获取权限菜单
      this.getRoleList();
      //打开页面
      this.lookWindow = true;
    },
    //打开编辑页面
    openEditWindow(item) {
      this.editWindow = true; //打开编辑菜单界面
      //清空回显数据
      this.editData = [];
      //回显数据
      this.editData = this.$CopyObject(item);
      //清空父级菜单
      this.parentMenus = [];
      //获取父菜单
      this.getParentMenu();
      //获取权限列表
      this.getRoleList();
    },

    //增加菜单
    insertMenu() {
      const data = {
        menuBo: this.$CopyObject(this.insertData),
        pageBo: this.$getPageInfo(this.pageSize, this.pageIndex)
      }
      this.$http.post("/menu/insert", data).then((res) => {
        //关闭窗口
        this.lookWindow = false;
        //分页操作
        this.cutDataList(res);
        //更新路由
        setRouter()
      });
    },
    //修改菜单
    updateMenu() {
      const data = {
        menuBo: this.editData,
        pageBo: this.$getPageInfo(this.pageSize, this.pageIndex)
      }
      this.$http.post("/menu/update", data).then((res) => {
        //关闭窗口
        this.editWindow = false;
        //分页操作
        this.cutDataList(res)
        //更新路由
        setRouter()
      });
    },
    //删除菜单
    deleteMenu(item) {
      this.$confirm("是否确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          const data = {
            menuBo: item,
            pageBo: this.$getPageInfo(this.pageSize, this.pageIndex)
          }
          this.$http.post("/menu/delete", data).then((res) => {
            //分页操作
            this.cutDataList(res)
            //更新路由
            setRouter()
          });
        })
        .catch(() => {
          this.$message.info("已取消删除");
        });
    },

    //查询所有数据
    getDataList() {
      this.dataListFrom = "getDataList";
      const pageInfo = this.$getPageInfo(this.pageSize, this.pageIndex)
      this.$http.post("/menu/getPage", pageInfo).then((res) => {
        this.cutDataList(res);
      });
    },
    //查询单条数据
    getDataListBySearch() {
      this.dataListFrom = "getDataListBySearch";
      const data = {
        menuBo: this.searchData,
        pageBo: this.$getPageInfo(this.pageSize, this.pageIndex)
      }
      this.$http.post("/menu/getPage/search", data).then((res) => {
        this.cutDataList(res);
      })
    },
    //获取父菜单
    getParentMenu() {
      this.$http.post("/menu/getList/MenuParent").then((res) => {
        this.parentMenus = res;
        this.parentMenus.push({menuId: 0, name: "无目录"})
      });
    },
    //获取角色信息
    getRoleList() {
      this.$http.post("/role/getList/roleKind").then((res) => {
        this.roles = res;
      });
    },
    //获取菜单类型字典
    getType() {
      this.$http.post("/dict/getList/menuType").then(res => {
        this.types = this.$StrToJson(res);
      })
    },
    //获取菜单图标字典
    getIcon() {
      this.$http.post("/dict/getList/icon").then(res => {
        this.icons = this.$StrToJson(res);

        const len = this.icons.length //数组长度
        const num = 4 //数组数量
        const arr = []
        for (let i = 0; i < len; i++) {
          if (i < num) {
            arr.push([])
          }
          arr[i % num].push(this.icons[i])
        }
        this.icons = arr
      })
    },

    //分页操作
    cutDataList(res) {
      const data = this.$cutPageDataList(res)
      this.dataList = data.dataList
      this.totalPage = data.totalPage
    },

    //初始化页号
    setPageSize() {
      this.pageSizes = this.$getPageSize()
      this.pageSize = this.pageSizes[0]
    },
  },
  mounted() {
    this.setPageSize()
    //获取所有菜单
    this.getDataList();
    //获取父菜单
    this.getParentMenu();
    //导入icon图标
    this.getIcon();
    //导入types类型
    this.getType()
  },
};
</script>
<style lang="less">
</style>
